<template>
  <div class="app-container">
    <div class="flex-between">
      <el-input
        v-model="search"
        class="search-main"
        placeholder="请输入药品编号查询"
        prefix-icon="el-icon-search"
        @keyup.enter.native="handleSearch"
      />
    </div>
    <el-table
      :data="medicineList"
      style="width: 100%;"
      height="40vh"
      highlight-current-row
      @row-click="clickRow"
    >

      <el-table-column
        align="center"
        label="药品名称"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="药品编号"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="药品库存"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="厂家"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.factory }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="规格"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.spec }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="售价"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.sellingPrice }}
        </template>
      </el-table-column>
      <el-table-column
        align="header-center"
        label="最后一次录入时间"
      >
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div style="text-align:right;">
      <el-button
        type="danger"
        @click="handleCancel"
      >取消</el-button>
      <el-button
        type="primary"
        @click="handleConfirm"
      >确定</el-button>
    </div>
  </div>
</template>

<script>

import { getMedicines } from '@/api/medicine'
import { timestampToTime } from '@/utils/time'

const defaultMedicine = {
  key: '',
  name: '',
  code: '',
  number: '',
  factory_id: '',
  selling_price: '',
  purchase_price: '',
  remake: ''
}

export default {
  data() {
    return {
      medicine: Object.assign({}, defaultMedicine),
      medicineList: [],
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      search: '',
      pageNum: 1, // 默认每页显示1条数据
      pageSize: 10,
      loading: false,
      totalCount: 0,
      selectMedicine: {}
    }
  },
  created() {
    this.getMedicine({
      pageNum: this.pageNum,
      pageSize: this.pageSize
    })
  },
  methods: {
    async getMedicine(data) {
      const res = await getMedicines(data)
      this.medicineList = res.data.list
      for (const i in this.medicineList) {
        this.medicineList[i].key = i
        this.medicineList[i].updateTime = timestampToTime(this.medicineList[i].updateTime)
      }
      this.totalCount = res.data.total
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    handleCancel() {
      this.$emit('cancel')
    },
    handleConfirm() {
      console.log(this.selectMedicine)
      this.$emit('confirm', this.selectMedicine)
    },
    handleSizeChange(page) {
      this.pageSize = page
      this.getUser({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.getMedicine({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    clickRow(row) {
      this.selectMedicine = row
    },
    handleSearch() {
      this.getMedicine({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        code: this.search
      })
    }
  }
}
</script>

<style lang="scss" scope>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
.el-table__body tr.current-row > td {
  background-color: rgb(64, 158, 255) !important;
  color: #fff;
}
</style>
